package com.shop.service.Impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.mapper.DtsAddressMapper;
import com.shop.mapper.DtsRegionMapper;
import com.shop.pojo.DtsAddress;
import com.shop.pojo.DtsRegion;
import com.shop.service.DtsAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName DtsAddressServiceImpl
 * @Author xiao宋
 * @Date 19:31  2022/10/15
 * @Version 1.0
 * @Description TODO
 */
@Service
public class DtsAddressServiceImpl implements DtsAddressService {
    @Autowired
    DtsAddressMapper addressMapper;
    @Autowired
    DtsRegionMapper regionMapper;

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String name, Integer userId) {
        IPage<DtsAddress> iPage = new Page<>(page, limit);
        QueryWrapper<DtsAddress> w = new QueryWrapper<>();
//        前端显示的用户名为 nickname。  并不是username
        if (!StringUtils.isEmpty(name)) {
            w.eq("name", name);
        }
        if (userId != null) {
            w.eq("user_id", userId);
        }
        //        排序方式
        if ("desc".equalsIgnoreCase(order)) {
            w.orderByDesc(sort);
        } else {
            w.orderByAsc(sort);
        }
        addressMapper.selectPage(iPage, w);
        Map<String, Object> data = new HashMap<>();
        data.put("total", iPage.getTotal());
        data.put("items", iPage.getRecords());
        return data;
    }

    @Override
    public Map<String, Object> findByAdministrativeDivisionPage(Integer page, Integer limit, String name, Integer code) {
        IPage<DtsRegion> iPage = new Page<>(page, limit);
        QueryWrapper<DtsRegion> w = new QueryWrapper<>();
//        前端显示的用户名为 nickname。  并不是username
        if (!StringUtils.isEmpty(name)) {
            w.eq("name", name);
        }
        if (code != null) {
            w.eq("code", code);
        }
//              行政区域没有要求排序方式

//        if ("desc".equalsIgnoreCase(order)) {
//            w.orderByDesc(sort);
//        } else {
//            w.orderByAsc(sort);
//        }
        regionMapper.selectPage(iPage, w);

        Map<String, Object> result = new HashMap<>();
        result.put("total", iPage.getTotal());
        result.put("items", iPage.getRecords());
        return result;
    }
}
